package com.itany.service.impl;

import com.itany.entity.*;
import com.itany.mapper.PermissionMapper;
import com.itany.mapper.RoleMapper;
import com.itany.mapper.RolePermissionMapper;
import com.itany.mapper.UserRoleMapper;
import com.itany.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper mapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name) {
        List<Role> list = mapper.selectByParams(name);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()){
            for (Role role : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",role.getId());
                u.put("name",role.getName());
                List<Permission> permissions = mapper.selectByRoleId(role.getId());
                u.put("permissions",permissions);
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void insertRole(String name, String ids) {
        Role role = new Role();
        role.setName(name);
        mapper.insertSelective(role);
        String[] list = ids.split(",");
        for(String id : list){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleid(role.getId());
            rolePermission.setPermissionid(Integer.parseInt(id));
            rolePermissionMapper.insertSelective(rolePermission);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryById(Integer id) {
        Role role = mapper.selectByPrimaryKey(id);
        Map<String, Object> u = new HashMap<>();
        if(role != null){
            u.put("id",role.getId());
            u.put("name",role.getName());
            RolePermissionExample example = new RolePermissionExample();
            example.or().andRoleidEqualTo(id);
            List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(example);
            List<Integer> ids = new ArrayList<>();
            if(!rolePermissions.isEmpty()){
                for(RolePermission rolePermission : rolePermissions){
                    ids.add(rolePermission.getPermissionid());
                }
            }
            List<Permission> list = permissionMapper.selectNull();
            List<Map<String, Object>> mapList = new ArrayList<>();
            if(!list.isEmpty()){
                for(Permission permission : list){
                    PermissionExample permissionExample = new PermissionExample();
                    permissionExample.or().andParentidEqualTo(permission.getId());
                    List<Permission> list1 = permissionMapper.selectByExample(permissionExample);
                    Map<String, Object> a = new HashMap<>();
                    a.put("id",permission.getId());
                    a.put("text",permission.getName());
                    a.put("state","closed");
                    List<Map<String, Object>> mapList1 = new ArrayList<>();
                    if(!list1.isEmpty()){
                        for(Permission permission1 : list1){
                            Map<String, Object> map = new HashMap<>();
                            map.put("id",permission1.getId());
                            map.put("text",permission1.getName());
                            if(ids.contains(permission1.getId())){
                                map.put("checked",true);
                            }
                            mapList1.add(map);
                        }
                        a.put("children",mapList1);
                    }else{
                        a.put("children",null);
                        if(ids.contains(permission.getId())){
                            a.put("checked",true);
                        }
                    }
                    mapList.add(a);
                }
                u.put("permissions",mapList);
            }else{
                u.put("permissions",null);
            }
            return u;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyRole(String name, String ids,Integer id) {
        Role role = new Role();
        role.setId(id);
        role.setName(name);
        mapper.updateByPrimaryKeySelective(role);
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.or().andRoleidEqualTo(id);
        rolePermissionMapper.deleteByExample(rolePermissionExample);
        String[] list = ids.split(",");
        for(String i : list){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleid(id);
            rolePermission.setPermissionid(Integer.parseInt(i));
            rolePermissionMapper.insertSelective(rolePermission);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String,Object> queryByPermissions(Integer id) {
        Map<String, Object> u = new HashMap<>();
        List<Permission> permissions = mapper.selectByRoleId(id);
        u.put("permissions",permissions);
        return u;
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryRoles() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Role> roles = mapper.selectByExample(new RoleExample());
        if (!roles.isEmpty()){
            for(Role role:roles){
                Map<String,Object> u = new HashMap<>();
                u.put("id",role.getId());
                u.put("text",role.getName());
                list.add(u);
            }
            return list;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Boolean deleteRole(Integer id) {
        UserRoleExample example = new UserRoleExample();
        example.or().andRoleidEqualTo(id);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        if(userRoles.isEmpty()){
            RolePermissionExample rolePermissionExample = new RolePermissionExample();
            rolePermissionExample.or().andRoleidEqualTo(id);
            rolePermissionMapper.deleteByExample(rolePermissionExample);
            mapper.deleteByPrimaryKey(id);
            return true;
        }else{
            return false;
        }
    }
}
